package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DsStudent;
import com.ruoyi.system.service.IDsStudentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生信息Controller
 *
 * @author tyro
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/system/student")
public class DsStudentController extends BaseController
{
    @Autowired
    private IDsStudentService dsStudentService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(DsStudent dsStudent)
    {
        startPage();
        List<DsStudent> list = dsStudentService.selectDsStudentList(dsStudent);
        return getDataTable(list);
    }

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    @PostMapping("/listByClasId")
    public AjaxResult llistByClasIdist(@RequestBody DsStudent dsStudent)
    {
        List<DsStudent> list = dsStudentService.selectDsStudentList(dsStudent);
        return success(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsStudent dsStudent)
    {
        List<DsStudent> list = dsStudentService.selectDsStudentList(dsStudent);
        ExcelUtil<DsStudent> util = new ExcelUtil<DsStudent>(DsStudent.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") String studentNo)
    {
        return success(dsStudentService.selectDsStudentByStudentId(studentNo));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DsStudent dsStudent)
    {
        return toAjax(dsStudentService.insertDsStudent(dsStudent));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DsStudent dsStudent)
    {
        return toAjax(dsStudentService.updateDsStudent(dsStudent));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable String[] studentIds)
    {
        return toAjax(dsStudentService.deleteDsStudentByStudentIds(studentIds));
    }


    /**
     * 导入学生数据
     */
    @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,
                                 @RequestParam(required = false) Long clasId,
                                 @RequestParam(required = false) String className) throws Exception
    {
        if (file == null) {
            return error("请选择要导入的文件");
        }

        try {
            ExcelUtil<DsStudent> util = new ExcelUtil<DsStudent>(DsStudent.class);
            // 更新索引数组，包含所需列
            List<DsStudent> studentList = util.importSpecificColumns(file,
                new int[]{0, 1, 2, 3, 4},
                new String[]{"studentNo", "studentName", "studentPassword", "studentSex", "className"});

            // 如果提供了班级ID，则为所有学生设置班级信息
            if (clasId != null) {
                for (DsStudent student : studentList) {
                    student.setClasId(clasId);
                    if (StringUtils.isEmpty(student.getClassName())) {
                        student.setClassName(className);
                    }
                }
            }

            String operName = getUsername();
            String message = dsStudentService.importStudentData(studentList, operName);
            return success(message);
        } catch (ServiceException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            System.out.println("导入学生数据失败"+ e);
            return error("导入学生数据失败，请联系管理员");
        }
    }



    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<DsStudent> util = new ExcelUtil<DsStudent>(DsStudent.class);
        util.importTemplateExcel(response, "学生数据");
    }


    /**
     * 获取所有班级名称列表
     */
    @GetMapping("/classnames")
    public AjaxResult getAllClassNames()
    {
        List<String> classNames = dsStudentService.selectAllClassNames();
        return success(classNames);
    }

    /**
     * 根据班级名称获取学生列表
     */
    @GetMapping("/byclass/{className}")
    public AjaxResult getStudentsByClassName(@PathVariable("className") String className)
    {
        List<DsStudent> students = dsStudentService.selectStudentsByClassName(className);
        return success(students);
    }

    /**
     * 查询学生信息
     */
    @PostMapping("/byUserName")
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    public AjaxResult getStudentId(@RequestBody DsStudent dsStudent)
    {
        startPage();
        DsStudent Student = dsStudentService.selectDsStudentIdByUserName(dsStudent);
        return success(Student);
    }
}
